package com.xk857.simpleeffect.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.dialog.CustomDatePickerDialog;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Matter;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.CalendarUtils;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.toast.XToast;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.function.Consumer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

public class UpdateMatterActivity extends AppCompatActivity {

    private TitleBar titleBar;
    private static final SimpleDateFormat canalSdf = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
    private EditText titleEdit;
    private EditText remarkEdit;
    private Matter matter;
    private RadioGroup radioGroup;

    private Calendar matterCalender;
    private TextView dateText;
    private LinearLayout matterDateLayout;

    private final int[] radios = {R.id.rb_button0, R.id.rb_button1, R.id.rb_button2, R.id.rb_button3, R.id.rb_button4, R.id.rb_button5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_add_matter);
        initView();
        initData();
    }

    private void initData() {
        // 根据id查询
        long id = getIntent().getLongExtra("id", -1);
        if (id == -1) {
            new MaterialDialog.Builder(this).iconRes(R.drawable.icon_info).title(R.string.info).content("请传递id到此页面").positiveText(R.string.yes)
                    .onPositive((dialog, which) -> finish()).show();
        }

        // 获取数据
        Type type = new TypeToken<Result<Matter>>() {}.getType();
        String url = OkHttpUtils.BASE_URL + "/api/v1/matter/" + id;
        OkHttpUtils.getInstance().get(this, url, type, true, (Consumer<Matter>) m -> {
            this.matter = m;
            Date matterDate = DateUtil.parse(matter.getDateTxt(), "yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(matterDate);
            matterCalender = calendar;
            runOnUiThread(() -> {
                dateText.setText(CalendarUtils.getDataStr(matterCalender, "yyyy-MM-dd"));
                radioGroup.check(radios[Integer.parseInt(matter.getIcon())]);
                titleEdit.setText(matter.getTitle());
                remarkEdit.setText(matter.getRemark());
            });
        });

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton radioButton = group.findViewById(checkedId);
            int i = group.indexOfChild(radioButton);
            matter.setIcon(String.valueOf(i));
        });
        matterDateLayout.setOnClickListener(v -> {
            CustomDatePickerDialog datePickerDialog = new CustomDatePickerDialog(this, com.xuexiang.xui.R.style.XUITheme_AlertDialog, (view, year, month, day) -> {
                Calendar cale = Calendar.getInstance();
                cale.set(year, month, day);
                matterCalender = cale;
                matter.setDateTxt(canalSdf.format(matterCalender.getTime()));
                dateText.setText(CalendarUtils.getDataStr(matterCalender, "yyyy-MM-dd"));
            }, matterCalender.get(Calendar.YEAR), matterCalender.get(Calendar.MONTH), matterCalender.get(Calendar.DAY_OF_MONTH), Calendar.getInstance());
            datePickerDialog.show();
        });
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setTitle("修改记录");
        titleBar.setLeftClickListener(v -> finish());
        radioGroup = findViewById(R.id.radio_group);

        titleEdit = findViewById(R.id.title_edit);
        remarkEdit = findViewById(R.id.remark_edit);
        dateText = findViewById(R.id.date_text);
        matterDateLayout = findViewById(R.id.matter_date_layout);
        Button addBtn = findViewById(R.id.add_btn);
        addBtn.setText(R.string.update_matter);
        addBtn.setOnClickListener(v -> {
            if (StrUtil.hasBlank(titleEdit.getText().toString())) {
                XToast.info(this, "记录名称不可为空").show();
                return;
            }
            matter.setRemark(remarkEdit.getText().toString());
            matter.setTitle(titleEdit.getText().toString());
            Type type = new TypeToken<Result<Matter>>() {
            }.getType();
            OkHttpUtils.getInstance().put(this, RequestEnum.ADD_MATTER, type, matter, true, (Consumer<Matter>) matter1 -> {
                finish();
            });
        });
    }
}